//
//  AssetView.m
//  AppleMarket
//
//  Created by yangy on 26/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "AssetView.h"

// view
#import "Row2Label.h"
// viewModel
#import "AssetViewModel.h"
#import "CoinAssetModel.h"

@interface AssetView()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *blockView; // 内容块视图
@property (nonatomic, strong) UIView *hLineView;
@property (nonatomic, strong) UIView *vLineView;
@property (nonatomic, strong) UIButton *eyeButton;
@property (nonatomic, strong) NSArray<Row2Label*> *rowLabels;
@property (nonatomic, strong) AssetViewModel *viewModel;
@end

@implementation AssetView
#define kLabelTag 100
- (instancetype)initWithFrame:(CGRect)frame viewModel:(id) viewModel
{
    self = [super initWithFrame:frame];
    if(self){
        _viewModel = viewModel;
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.titleLabel];
        [self addSubview:self.eyeButton];
        [self addSubview:self.blockView];
        [self.blockView addSubview:self.hLineView];
        [self.blockView addSubview:self.vLineView];
        [self setViewLayout];
        [self initAssetLabel];
        [self bindViewModel];
    }
    return self;
}

- (void) initAssetLabel
{
    NSArray *titles = @[@"币币账号",@"法币账户",@"合约账户"];
    [titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        Row2Label *label = [[Row2Label alloc] initWithFrame:CGRectZero];
        [label setTopText:obj image:[UIImage imageNamed:@"right_icon"]];
        [label setTag:kLabelTag + idx];
        [[label rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            NSLog(@"币币账户");
            [self.viewModel.assetClickSubject sendNext:@(idx)];
        }];
        [self.blockView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.blockView.mas_width).multipliedBy(0.5);
            make.height.mas_equalTo(self.blockView.mas_height).multipliedBy(0.5);
            if(idx % 2 == 0){
                make.left.mas_equalTo(self.blockView);
            }else{
                make.left.mas_equalTo(self.vLineView.mas_right);
            }
            if(idx / 2 != 0){
                make.top.mas_equalTo(self.hLineView.mas_top);
            }else{
                make.top.mas_equalTo(self.blockView);
            }
        }];
    }];
}

- (void) bindViewModel
{
    [self.viewModel.refershEndSubject subscribeNext:^(id  _Nullable x) {
        [self updateAssetLabel];
    }];
}

- (void) updateAssetLabel
{
    // 刷新用户资产
    CoinAssetModel *m = [[[self.viewModel.bbConinModels.rac_sequence filter:^BOOL(CoinAssetModel  *value) {
        // 找到apple币种
        return [value.Unit isEqualToString:@"APPLE"];
    }] map:^id _Nullable(CoinAssetModel * _Nullable value) {
        return value;
    }] head];
    
    NSString *name = @"APPLE";
    
    NSString *text = [NSString stringWithFormat:@"%.6f %@",m.Available,name];
    if([PlistCommon hiddenAsset]){
        text = [NSString stringWithFormat:@"%@ %@",@"*.******",name];
    }
    NSMutableAttributedString *attrText = [[NSMutableAttributedString alloc] initWithString:text];
    [attrText addAttribute:NSFontAttributeName value:kFont(11) range:[text rangeOfString:name]];
    [attrText addAttribute:NSForegroundColorAttributeName value:kColorWithHex(0x80878E) range:[text rangeOfString:name]];
    
    // 币币账户
    Row2Label *label = [self.blockView viewWithTag:kLabelTag];
    // 法币账户
    Row2Label *label2 = [self.blockView viewWithTag:kLabelTag + 1];
    // 合约账户
    Row2Label *label3 = [self.blockView viewWithTag:kLabelTag + 2];
    
    // 显示apple币种余额
    [label setDownAttrText:attrText];
    [label2 setDownAttrText:attrText];
    [label3 setDownAttrText:attrText];
}

- (void) setViewLayout
{
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.top.mas_equalTo(self).offset(25);
    }];
    
    [_eyeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY);
    }];
    
    [_blockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(16);
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-15);
        make.bottom.mas_equalTo(self).offset(-22);
    }];
    
    [_hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.blockView);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(self.blockView);
        make.right.mas_equalTo(self.blockView);
    }];
    
    [_vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.blockView);
        make.top.mas_equalTo(self.blockView);
        make.bottom.mas_equalTo(self.blockView);
        make.width.mas_equalTo(1);
    }];
}

- (UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [UILabel cm_labelWithText:@"我的资产" font:kFontBold(16) textColor:kColorWithHex(0x0E304C)];
    }
    return _titleLabel;
}

- (UIView *)blockView
{
    if(!_blockView){
        _blockView = [[UIView alloc] init];
        _blockView.backgroundColor = [UIColor whiteColor];
        _blockView.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.16].CGColor;
        _blockView.layer.shadowOffset = CGSizeMake(0, 3);
        _blockView.layer.shadowRadius = 3;
        _blockView.layer.shadowOpacity = 1;
    }
    return _blockView;
}

- (UIView *)hLineView
{
    if(!_hLineView){
        _hLineView = [[UIView alloc] init];
        _hLineView.backgroundColor = kColorWithHex(0xF4F8FB);
    }
    return _hLineView;
}
- (UIView *)vLineView
{
    if(!_vLineView){
        _vLineView = [[UIView alloc] init];
        _vLineView.backgroundColor = kColorWithHex(0xF4F8FB);
    }
    return _vLineView;
}

- (UIButton *)eyeButton
{
    if(!_eyeButton){
        _eyeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_eyeButton setImage:[UIImage imageNamed:@"eye_icon"] forState:UIControlStateNormal];
        [_eyeButton setImage:[UIImage imageNamed:@"eye_sel"] forState:UIControlStateSelected];
        @weakify(self);
        [[_eyeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            //@strongify(self);
            x.selected = !x.isSelected;
            // 显示、隐藏账户资产
            [PlistCommon saveHiddenAsset:x.isSelected];
            [self updateAssetLabel];
        }];
    }
    return _eyeButton;
}

@end
